<template>
  <div>
    <tableComponents
      :isHaveExport="false"
      :isShow="false"
      :isHavePagination="false"
      @toAdd="add_stage"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="stage_list"
          v-loading="loading"
          element-loading-text="拼命加载中"
          stripe
          header-row-class-name="head_color"
          tooltip-effect="dark"
          :default-sort="{prop: 'sort',}"
          style="width: 99.8%"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" class="tc" width="35"></el-table-column>
          <el-table-column prop="sort" sortable :sort-method="sorta" label="排序" min-width="55">
            <template slot-scope="scope">
              <el-input
                @change="sortNew(scope.$index,scope.row)"
                v-model.number="scope.row.sort"
                autocomplete="off"
                size="small"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column type="selection"></el-table-column>
          <el-table-column label="课程ID" min-width="120">
            <template slot-scope="scope">{{scope.row.id }}</template>
          </el-table-column>
          <el-table-column label="课程名称" min-width="300">
            <template slot-scope="scope">{{scope.row.title }}</template>
          </el-table-column>
          <el-table-column label="课程类型" min-width="80">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.is_text=='1'" type="success" effect="dark">视频</el-tag>
              <el-tag v-if="scope.row.is_text=='0'" type="info" effect="dark">图文</el-tag>
            </template>
          </el-table-column>

          <el-table-column label="是否付费" min-width="130" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-switch
                class="zdySwith"
                :width="82"
                v-model="scope.row.is_free"
                active-color="#13ce66"
                inactive-color="#ffffff"
                active-text="收费"
                inactive-text="免费"
                active-value="1"
                @change="getIsfree($event,scope.row)"
                inactive-value="0"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="点击量" min-width="50">
            <template slot-scope="scope">{{scope.row.hit || 0 }}</template>
          </el-table-column>
          <el-table-column label="创建时间" min-width="120">
            <template slot-scope="scope">{{scope.row.created_time|getTimeFormat}}</template>
          </el-table-column>
          <el-table-column label="操作" min-width="120">
            <template slot-scope="scope">
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="primary"
                :disabled="scope.row.video !=0 && scope.row.video != '' ? false:true"
                @click="playVideo(scope.row)"
              >查看视频</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="primary"
                @click="update_stage(scope.row)"
              >编辑</el-button>
              <el-button
                :loading="$store.state.isLoading"
                size="mini"
                type="danger"
                @click="delete_stage( scope.row.id)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template v-slot:footerLeft>
        <a class="aBtn aBtn_1 aBtn_lan2" @click="deleteAll_robots">删除</a>
      </template>
    </tableComponents>

    <el-drawer
      class="stage-el-drawer__body"
      :append-to-body="true"
      size="80%"
      :visible.sync="dialogaddVisible"
      direction="ltr"
    >
      <add-stage :item="item" v-if="dialogaddVisible" @openThis="openThis" />
    </el-drawer>
  </div>
</template>
<script>
import addStage from "./add-stage";
export default {
  name: "stage",
  data() {
    return {
      multipleSelection: [],
      course_id: this.$route.query.id,
      is_add: true,
      dialogaddvideoVisible: false,
      itemForm: {
        video: "",
        title: ""
      },
      showVideo: false,
      add_form: {
        lesson_id: "",
        title: "",
        video: "",
        is_free: "0",
        is_text: "1",
        content: ""
      },
      stage_single: {},
      dialogaddVisible: false,
      dialogFormVisible: false,
      dioLoading: false,

      stage_list: [],
      loading: false,

      serarch_from: {},
      formLabelWidth: "120px",
      item: {}
    };
  },
  components: {
    addStage
  },
  created() {
    this.get_stage_list();
  },
  methods: {
    sortNew(index, row) {
      var params = {};
      params = row;
      params.sort = row.sort;
      this.$api.getShops.edit_stagelist(params).then(res => {
        if (res.data.code == 200 && res.data.result) {
          this.get_stage_list(this.serarch_from);
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
        }
      });
    },
    sorta(a, b) {
      return b - a;
    },
    openThis() {
      this.get_stage_list();
    },
    getIsfree(val, row) {
      var params = {};
      params = row;
      params.is_free = val;
      this.$api.getShops.edit_stagelist(params).then(res => {
        if (res.data.code == 200 && res.data.result) {
          return false;
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result); //失败弹出框
        }
      });
    },
    deleteAll_robots() {
      //批量删除
      var str = "";
      if (this.multipleSelection.length <= 0) {
        this.$message.error("未选择批量删除");
      } else {
        for (var i in this.multipleSelection) {
          str += this.multipleSelection[i].id + "@";
        }
        str = str.slice(0, -1);
        this.$swal({
          title: "您确定要删除这些信息吗",
          text: "删除后将无法恢复，请谨慎操作！",
          type: "warning",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "是的，我要删除",
          cancelButtonText: "容我三思"
        }).then(willDelete => {
          if (willDelete.value) {
            this.$api.getShops.del_stagelist({ id_str: str }).then(res => {
              if (res.data.code == 200) {
                this.$swal("删除成功！", "您已经删除了这条信息。", "success");
                this.get_stage_list();
              } else {
                this.$swal("删除失败！", "出错啦！", "success");
                this.get_stage_list();
              }
            });
          }
        });
      }
    },
    delete_stage(id) {
      //删除
      this.$swal({
        title: "您确定要删除这些信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          this.$api.getShops.del_stagelist({ id_str: id }).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.get_stage_list();
            } else {
              this.$swal("删除失败！", res.data.result, "error");
              this.get_stage_list();
            }
          });
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    close_video() {
      this.dialogaddvideoVisible = false;
      if (this.is_add) {
        this.dialogaddVisible = true;
      } else {
        this.dialogFormVisible = true;
      }
      this.$refs.video_plugin.close_video();
    },
    add_stage() {
      this.item = {};
      this.dialogaddVisible = true;
    },
    update_stage(item) {
      this.item = item;
      this.dialogaddVisible = true;
    },
    receive_search(form) {
      this.serarch_from = form;
      this.get_stage_list(form);
    },

    get_stage_list(data) {
      this.loading = true;
      var params = {};
      data = data || {};
      params = data;
      params.id = this.$route.query.id;
      this.$api.getShops.get_stagelist(params).then(res => {
        if (res.data.code == 200) {
          this.stage_list = res.data.result;
          this.loading = false;
        } else {
          this.$fnc.alertError(res.data.result);
          this.loading = false;
        }
      });
    }
  }
};
</script>
<style scoped>
.vide0-con {
  background-color: transparent !important;
  border: none;
  box-shadow: none;
  width: 600px;
}
.vide0-con .el-dialog__header {
  display: none;
}
</style>
